package com.totoro.controller.request;

import com.totoro.pojo.*;
import com.totoro.service.IndexService;
import com.totoro.service.ProductService;
import com.totoro.utils.MapSort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.*;

/**
 * @author TOTORO
 * @date 2021/8/21 12:15
 */
@Controller
public class IndexRequestController {
    @Autowired
    ProductService productService;
    @Autowired
    private IndexService indexService;

    @RequestMapping("/")
    public String index(Model model, HttpSession session, HttpServletRequest request){
        System.out.println("首页");
        String uploadPath = request.getSession().getServletContext().getRealPath("/")+"static\\img\\index\\carousel\\";
        String[] carouselImg = new File(uploadPath).list();
        model.addAttribute("carouselImages", carouselImg);
        // 获得搜索排名最高的搜索词
        List<SearchTextList> searchTextLists = productService.findSearchText(null);
        model.addAttribute("searchTextLists", searchTextLists);
        // 获得所有首页分类
        Map<IndexCategory, Map<IndexCategory, List<IndexCategory>>> categoryMapMap = new LinkedHashMap<>();
        // 先获取一级分类
        List<IndexCategory> oneCategory = indexService.findAllOneCategory();
        // 循环将第一级分类值给集合键
        oneCategory.forEach(v->{
            categoryMapMap.put(v, new LinkedHashMap<IndexCategory, List<IndexCategory>>());
        });
        // 循环集合，获取二级分类
        categoryMapMap.forEach((k,v) ->{
            List<IndexCategory> twoCategory = indexService.findAllTwoCategoryByOneCategoryId(k.getId());
            // 循环二级分类，添加value值的key
            twoCategory.forEach(tc->{
                // 获取三级分类
                List<IndexCategory> threeCategory = indexService.findAllThreeCategoryByTwoCategoryId(tc.getId());
                categoryMapMap.get(k).put(tc,threeCategory);
                // 将三级分类集合赋予value->value
            });
        });
        // 将分类添加至作用域
        model.addAttribute("categoryMapMap", categoryMapMap);

        // 查询推荐商品
        // 推荐思路：首先查询最近一个月的随机几条用户足迹（浏览过的商品）做分割关键字查询，
        // 再按照销量最高的随机几条商品记录
        // 其次是以搜索最高的商品记录
//        User user = (User) session.getAttribute("user");
//        List<MyHistory> myHistories = new ArrayList<>();
//        if(null != user){
//            myHistories = productService.findRecentRandHistoryProducts(user.getUserId());
//        }
//        List<Product> finalProducts = new ArrayList<>();
//        for (MyHistory myHistory : myHistories) {
//            finalProducts.add(myHistory.getProduct());
//        }
//        List<Product> products1 = productService.findHotRandHistoryProducts();
//        List<Product> products2 = productService.findKeywordAgoProducts();
//
//        finalProducts.addAll(products1);
//        finalProducts.addAll(products2);

        // 获取信誉分最强的前四个店铺的三个商品
        Map<Shop, List<Product>> shopListMap = indexService.findMaxDealProductByShop();
        model.addAttribute("shopListMap", shopListMap);
            return "index";
//        return "forward:index.jsp";
    }
}
